package com.aauedu.springboot.config;

import com.aauedu.springboot.controller.LoginController;
import com.aauedu.springboot.interceptor.LoginInterceptor;
import com.aauedu.springboot.interceptor.LoginInterceptor2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//方式二：直接实现WebMvcConfigurer接口
public class WebConfig implements WebMvcConfigurer{
//public class WebConfig {

    //方式一：
    //注入Bean WebMvcConfigurer
    /*@Bean
    public WebMvcConfigurer webMvcConfigurer () {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                LoginInterceptor loginInterceptor = new LoginInterceptor();
                registry.addInterceptor(loginInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns("/", "/login", "/images/**");
            }
        };
    }*/

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器LoginInterceptor
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")  //拦截所有的请求
                .excludePathPatterns("/login", "/", "/images/**", "/upload.html", "/upload", "/xxx", "/welcome", "/sql");  //指定要放行的，后面可以根据业务需要，指定具体的路径
    }
}
